package cn.edu.scau.cmi.chenjia.comprehensive.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.io.FileOutputStream;
import java.lang.String;
import java.text.DecimalFormat;
import java.util.HashSet;

import cn.edu.scau.cmi.chenjia.comprehensive.Main;
import cn.edu.scau.cmi.chenjia.comprehensive.model.Student;

public class StudentOverviewController {
   
    @FXML
    private ChoiceBox<String> subject;
    @FXML
    private ChoiceBox<String> teacher;
    @FXML
    private ChoiceBox<String> classs;
    @FXML
    private TableView<Student> studentTable;
    @FXML
    private TableColumn<Student, String> numberColumn;
    @FXML
    private TableColumn<Student, String> nameColumn;
    @FXML
    private TableColumn<Student, String> workColumn;
    @FXML
    private TableColumn<Student, String> testColumn;
    @FXML
    private TableColumn<Student, String> homeworkColumn;
    @FXML
    private TableColumn<Student, String> termColumn;
    @FXML
    private TableColumn<Student, String> commentColumn;

    @FXML
    private Label a1;
    @FXML
    private Label b1;
    @FXML
    private Label c1;
    @FXML
    private Label d1;
    @FXML
    private Label e1;
    @FXML
    private Label a2;
    @FXML
    private Label b2;
    @FXML
    private Label c2;
    @FXML
    private Label d2;
    @FXML
    private Label e2;

    private Main main;

    public StudentOverviewController() {
    }
    
    @FXML
    private void initialize() {
        numberColumn.setCellValueFactory(cellData -> cellData.getValue().numberProperty());
        nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        workColumn.setCellValueFactory(cellData -> cellData.getValue().workProperty());
        testColumn.setCellValueFactory(cellData -> cellData.getValue().testProperty());
        homeworkColumn.setCellValueFactory(cellData -> cellData.getValue().homeworkProperty());
        termColumn.setCellValueFactory(cellData -> cellData.getValue().termProperty());
        commentColumn.setCellValueFactory(cellData -> cellData.getValue().commentProperty());
    }
    
    public void setMain(Main main) {
    	this.main=main;
    }
    
    public void setSubject() {
    	HashSet<String> sj = new HashSet<>();
		ObservableList<Student> data = main.getStudentData();
		for (int i = 0; i < data.size(); i++) {
			sj.add(data.get(i).getsubject());
		}

		ObservableList<String> data2 = FXCollections.observableArrayList();
		for (String aa : sj) {
			data2.add(aa);
		}
		subject.getItems().addAll(data2);
		subject.getSelectionModel().selectedItemProperty().addListener((ov, oldv, newv)->{
			teacher.getItems().clear();
			classs.getItems().clear();
			studentTable.getItems().clear();
			setTeacher(newv);
		});
    }
    
    
    public void setTeacher(String subject) {
    	HashSet<String> th = new HashSet<>();
		ObservableList<Student> data = main.getStudentData();
		for (int i = 0; i < data.size(); i++) {
			th.add(data.get(i).getteacher());
		}

		ObservableList<String> data2 = FXCollections.observableArrayList();
		for (String aa : th) {
			data2.add(aa);
		}
		teacher.getItems().addAll(data2);
		teacher.getSelectionModel().selectedItemProperty().addListener((ov, oldv, newv)->{
			classs.getItems().clear();
			studentTable.getItems().clear();
			setClasss(subject,newv);
		});
    }
    
    public void setClasss(String subject,String teacher) {
    	HashSet<String> cs = new HashSet<>();
		ObservableList<Student> data = main.getStudentData();
		for (int i = 0; i < data.size(); i++) {
			cs.add(data.get(i).getclasss());
		}

		ObservableList<String> data2 = FXCollections.observableArrayList();
		for (String aa : cs) {
			data2.add(aa);
		}
		classs.getItems().addAll(data2);
		classs.getSelectionModel().selectedItemProperty().addListener((ov, oldv, newv)->{
			studentTable.getItems().clear();
			setTable(subject,teacher,newv);
		});
    }
    
    public void setTable(String subject,String teacher,String classs) {
    	ObservableList<Student> data = main.getStudentData();
		ObservableList<Student> data2 = FXCollections.observableArrayList();
		double [][] count = new double[6][2];
		for (int i = 0; i < data.size(); i++) {
			Student dt=data.get(i);
			if(dt.getsubject().equals(subject)&&dt.getteacher().equals(teacher)&&dt.getclasss().equals(classs)) {
				count[0][0]++;
				data2.add(dt);
				if(Integer.parseInt(dt.getcomment())>=90) count[1][0]++;
				else if(Integer.parseInt(dt.getcomment())>=80) count[2][0]++;
				else if(Integer.parseInt(dt.getcomment())>=70) count[3][0]++;
				else if(Integer.parseInt(dt.getcomment())>=60) count[4][0]++;
				else count[5][0]++;
				}
		}
		for(int i=1;i<=5;i++) {
			count[i][1]=count[i][0]/count[0][0];
		}
		if (count[0][0] != 0) {
			
            a1.setText(String.valueOf(count[1][0]));
            b1.setText(String.valueOf(count[2][0]));
            c1.setText(String.valueOf(count[3][0]));
            d1.setText(String.valueOf(count[4][0]));
            e1.setText(String.valueOf(count[5][0]));
            a2.setText(String.valueOf(count[1][1]*100)+"%");
            b2.setText(String.valueOf(count[2][1]*100)+"%");
            c2.setText(String.valueOf(count[3][1]*100)+"%");
            d2.setText(String.valueOf(count[4][1]*100)+"%");
            e2.setText(String.valueOf(count[5][1]*100)+"%");

        } else {
        	a1.setText("");
            b1.setText("");
            c1.setText("");
            d1.setText("");
            e1.setText("");
            a2.setText("");
            b2.setText("");
            c2.setText("");
            d2.setText("");
            e2.setText("");
           
        }
		
		studentTable.getItems().clear();
		studentTable.getItems().addAll(data2);	
    }
   

    
}